﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Zen4SyncDashboard.DAL
{
    public partial class subscriber : IDataErrorInfo
    {
        #region - IDataErrorInfo Members -

        public string Error { get { return null; } }

        public string this[string columnName]
        {
            get { return GetValidationError(columnName); }
        }

        #endregion - IDataErrorInfo Members -

        #region - Validation -

        static readonly string[] ValidatedProperties = 
        {
            "subscriber_name",
            "subscriber_connectionString",
            "subscriber_sdfFileName",
            "subscriber_sdfFileDirectory",
            "subscriber_validateType"
        };

        public bool IsValid()
        {
            foreach (string property in ValidatedProperties)
                if (GetValidationError(property) != null)
                    return false;

            return true;
        }

        string GetValidationError(string propertyName)
        {
            if (!ValidatedProperties.Contains(propertyName))
                return null;

            string error = null;

            switch (propertyName)
            {
                case "subscriber_name":
                    error = ValidateName();
                    break;
                case "subscriber_connectionString":
                    error = ValidateConnectionString();
                    break;
                case "subscriber_sdfFileName":
                    error = ValidateFileName();
                    break;
                case "subscriber_sdfFileDirectory":
                    error = ValidateFileDirectory();
                    break;
                case "subscriber_validateType":
                    error = ValidateValidateType();
                    break;
                default:
                    throw new ArgumentException("Unrecognized property: " + propertyName);
            }

            return error;
        }

        private string ValidateValidateType()
        {
            if (validateType == null)
                return "A Validate Type must be selected";

            return null;
        }

        private string ValidateFileDirectory()
        {
            if (string.IsNullOrWhiteSpace(subscriber_sdfFileDirectory))
                return "File Directory can't be empty";

            return null;
        }

        private string ValidateFileName()
        {
            if (string.IsNullOrWhiteSpace(subscriber_sdfFileName))
                return "File Name can't be empty.";
            else if (!subscriber_sdfFileName.EndsWith(".sdf", StringComparison.InvariantCultureIgnoreCase))
                return "File must be .sdf type.";

            return null;
        }

        private string ValidateConnectionString()
        {
            if (string.IsNullOrWhiteSpace(subscriber_connectionString))
                return "Connection String can't be empty";

            return null;
        }

        private string ValidateName()
        {
            if (string.IsNullOrWhiteSpace(subscriber_name))
                return "Name can't be empty";

            return null;
        }

        #endregion - Validation -
    }
}
